package com.xinqi.modules.course.classmenu.controller.web;

import cn.hutool.core.collection.CollectionUtil;
import com.xinqi.common.base.enums.YesNoNumberEnum;
import com.xinqi.common.base.response.R;
import com.xinqi.common.core.enums.ClassMenuHideEnum;
import com.xinqi.common.core.utils.MessageUtils;
import com.xinqi.modules.course.classmenu.domain.ClassMenuEntity;
import com.xinqi.modules.course.classmenu.dto.req.ClassMenuQueryDTO;
import com.xinqi.modules.course.classmenu.dto.rsp.ClassMenuResultDTO;
import com.xinqi.modules.course.classmenu.manager.ClassMenuManager;
import com.xinqi.modules.course.classmenu.service.ClassMenuService;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * web课程中版块设置表(ClassMenu)表控制层
 *
 * @author liuxingxing
 * @since 2023-03-06 15:40:30
 */
@Tag(name = "Web-班级菜单", description = "班级菜单")
@Validated
@RestController
@RequestMapping("/web/classMenu")
@RequiredArgsConstructor
public class WebClassMenuController {
    /**
     * 服务对象
     */
    private final ClassMenuService classMenuService;
    private final ClassMenuManager classMenuManager;

    /**
     * 查询未隐藏的所有班级菜单
     *
     * @return 所有数据
     */
    @GetMapping("/unhide/{classId}")
    public R<List<ClassMenuResultDTO>> selectUnhide(@PathVariable("classId") Long classId) {
        ClassMenuQueryDTO query = new ClassMenuQueryDTO();
        query.setClassId(classId);
        query.setHidden(ClassMenuHideEnum.NO_HIDE);
        query.setSort(YesNoNumberEnum.YES.getCode());
        return R.ok(this.classMenuService.findList(query));
    }

    /**
     * 查询隐藏的所有班级菜单
     *
     * @return 所有数据
     */
    @GetMapping("/hide/{classId}")
    public R<List<ClassMenuResultDTO>> selectHide(@PathVariable("classId") Long classId) {
        ClassMenuQueryDTO query = new ClassMenuQueryDTO();
        query.setClassId(classId);
        query.setHidden(ClassMenuHideEnum.HIDE);
        return R.ok(this.classMenuService.findList(query));
    }


    /**
     * 批量修改多条数据
     *
     * @param classMenus 实体对象集合
     * @return 修改结果
     */
    @PostMapping("/updateBatch")
    public R<Void> updateBatch(@RequestBody List<ClassMenuEntity> classMenus) {
        if (CollectionUtil.isNotEmpty(classMenus)) {
            if (classMenuManager.updateBatchById(classMenus)) {
                return R.ok(MessageUtils.message("common.update.code.success"));
            } else {
                return R.error(MessageUtils.message("common.update.code.fail"));
            }
        }
        return R.error(MessageUtils.message("common.update.code.fail"));
    }

}
